﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Matriculex.Dominio;

namespace Matriculex.UI.Utils
{
    public static class HttpData
    {
        public static IEnumerable<string> AsKeys(string data, char separator)
        {
            if (data == null)
                return new string[0];

            return data.Split(separator);
        }

        public static IEnumerable<T> AsKeys<T>(string data)
            where T : IConvertible
        {
            return AsKeys<T>(data, ',');
        }

        public static IEnumerable<T> AsKeys<T>(string data, char separator)
            where T : IConvertible
        {
            var innerData = data ?? String.Empty;
            var values = innerData.Split( new char[]{ separator }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var value in values)
            {
                yield return (T) Convert.ChangeType(value, typeof (T));
            }
        }

        public static T As<T>(string data)
            where T : IConvertible
        {
            return (T)Convert.ChangeType(data, typeof(T));
        }

        public static string ValidateConfirm(string value, string confirmation, string exceptionMessage)
        {
            if (!Equals(value, confirmation))
                throw new ValidationException(exceptionMessage);

            return value;
        }
    }
}
